![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
@httptoolkit/esm
Advanced tools
Part of HTTP Toolkit: powerful tools for building, testing & debugging HTTP(S)
This is a forked version of the esm package: the brilliantly simple, babel-less, bundle-less ECMAScript module loader (which is now unmaintained). This fork includes:
node:*
imports.const require = createRequire(...)
pattern (or declare any other global require
variable) which are otherwise unusable with esm
.exports
map in their package.json, without a main
field, which are otherwise unresolveable (reporting "Cannot find module", even though import()
works correctly).Run npm i @httptoolkit/esm
or yarn add @httptoolkit/esm
.
There are two ways to enable @httptoolkit/esm
.
Enable @httptoolkit/esm
for packages:
Use @httptoolkit/esm
to load the main ES module and export it as CommonJS.
index.js
// Set options as a parameter, environment variable, or rc file.
require = require("@httptoolkit/esm")(module/*, options*/)
module.exports = require("./main.js")
main.js
// ESM syntax is supported.
export {}
Enable @httptoolkit/esm
for local runs:
node -r @httptoolkit/esm main.js
:bulb: Omit the filename to enable @httptoolkit/esm
in the REPL.
:clap: By default, :100: percent CJS interoperability is enabled so you can get stuff done.
:lock: .mjs
files are limited to basic functionality without support for @httptoolkit/esm
options.
Out of the box @httptoolkit/esm
just works, no configuration necessary, and supports:
import
/export
import.meta
import
stdin
, --eval
, --print
flags--check
flag (Node 10+)Specify options with one of the following:
"esm"
field in package.json
.esmrc.js
, .esmrc.cjs
, or .esmrc.mjs
file.esmrc
or .esmrc.json
fileESM_OPTIONS
environment variableESM_DISABLE_CACHE
environment variable{ | |||||||||||||||||||||
"cjs":true | A boolean or object for toggling CJS features in ESM. Features
| ||||||||||||||||||||
"mainFields":["main"] | An array of fields checked when importing a package. | ||||||||||||||||||||
"mode":"auto" | A string mode:
| ||||||||||||||||||||
"await":false | A boolean for top-level | ||||||||||||||||||||
"force":false | A boolean to apply these options to all module loads. | ||||||||||||||||||||
"wasm":false | A boolean for WebAssembly module support. (Node 8+) | ||||||||||||||||||||
} |
{ | |
"cache":true | A boolean for toggling cache creation or a cache directory path. |
"sourceMap":false | A boolean for including inline source maps. |
} |
For bundlers like browserify
+esmify
,
parcel-bundler
, and webpack
add a "module"
field to package.json
pointing to the main ES module.
"main": "index.js",
"module": "main.js"
:bulb: This is automagically done with npm init esm
or yarn create esm
.
@httptoolkit/esm
for wallaby.js
following their
integration example.Load @httptoolkit/esm
before loaders/monitors like
@babel/register
,
newrelic
,
sqreen
, and
ts-node
.
Load @httptoolkit/esm
for jasmine
using the
"helpers"
field in jasmine.json
:
"helpers": [
"node_modules/@httptoolkit/esm"
]
Load @httptoolkit/esm
with “node-args" options of:
pm2
: --node-args="-r @httptoolkit/esm"
Load @httptoolkit/esm
with “require” options of
ava
,
mocha
,
nodemon
,
nyc
,
qunit
,
tape
, and
webpack
.
:bulb: Builtin require
cannot sideload .mjs
files. However, .js
files
can be sideloaded or .mjs
files may be loaded with dynamic import
.
FAQs
Tomorrow's ECMAScript modules today!
The npm package @httptoolkit/esm receives a total of 2,423 weekly downloads. As such, @httptoolkit/esm popularity was classified as popular.
We found that @httptoolkit/esm demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.